﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GONES.IBLL;
using GONES.IDAL;
using GONES.Model;
using log4net;
using System.Reflection;
using Newtonsoft.Json;
using GONES.Tools.Common;
using System.Collections;

namespace GONES.BLL
{
    public class S_EmployeesManager : IS_EmployeesManager
    {
        protected static readonly ILog Log =
                LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private readonly IUserInfo _userInfo;
        public IS_EmployeesDao S_EmployeesDao;
        public IS_LogDao S_LogDao;
        public S_LogManager S_LogManager;

        public string ErrMsg { get; set; }

        public S_EmployeesManager(IUserInfo userinfo)
        {
            _userInfo = userinfo;
        }
        public S_Employees Get(string id)
        {
            return S_EmployeesDao.Get(id);
        }

        /// <summary>
        /// 新增雇员信息
        /// </summary>
        /// <param name="users">雇员信息</param>
        public void Add(S_Employees employees)
        {
            if (IsError(employees))
                return;
            employees.CreateTime = System.DateTime.Now; ;
            S_EmployeesDao.Save(employees);
            SaveLog("雇员管理", "雇员添加", "系统管理", employees); 
        }

        /// <summary>
        /// 修改雇员信息
        /// </summary>
        /// <param name="users">雇员信息</param>
        public void Edit(S_Employees employees)
        {
            if (IsError(employees))
                return;
            employees.ModifyTime = System.DateTime.Now; 
            S_EmployeesDao.Update(employees);
            SaveLog("雇员管理", "雇员修改","系统管理",employees); 
        }

        /// <summary>
        /// 删除雇员信息
        /// </summary>
        /// <param name="id">雇员ID</param>
        public void Delete(string id)
        {
            S_Employees employees = S_EmployeesDao.Get(id);
            S_EmployeesDao.Delete(employees);
            SaveLog( "雇员管理", "雇员删除","系统管理",employees); 
        }

        /// <summary>
        /// 获取雇员列表
        /// </summary>
        /// <param name="filters">条件</param>
        /// <param name="pageIndex">页号</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="recordCount">记录条数</param>
        /// <returns>雇员列表</returns>
        public IList<S_Employees> List(IList<DataFilter> filters, int pageIndex, int pageSize, ref long recordCount)
        {
            string strFilter = DataOperate.GetHqlstrByExtFilter(filters, "e");

            recordCount = S_EmployeesDao.ExecHqlResultLong("SELECT COUNT(*) FROM S_Employees e WHERE 1=1 " +
                                                  (string.IsNullOrEmpty(strFilter) ? string.Empty : " AND " + strFilter));

            return S_EmployeesDao.GetHqlList("SELECT e FROM S_Employees e WHERE 1=1 " +
                                          (string.IsNullOrEmpty(strFilter)
                                               ? string.Empty
                                               : " AND " + strFilter) + " ORDER BY e.ID DESC", (pageIndex - 1) * pageSize,
                                          pageSize);
        }

        private void SaveLog(string module, string operate, string logtype, S_Employees employees)
        {
            S_LogManager = new S_LogManager(_userInfo);
            string detail = string.Format("{0}:雇员名是{1}", operate, employees.EmployeeName);
            SaveLog(module, operate, logtype, detail);
            if (Log.IsInfoEnabled)
                Log.Info(string.Format("{0}：{1}", operate, JsonConvert.SerializeObject(employees)));
        }
        
        protected bool IsError(S_Employees employees)
        {
            if (string.IsNullOrEmpty(employees.EmployeeName))
            {
                ErrMsg = "雇员姓名不能为空！";
            }
            if (!string.IsNullOrEmpty(ErrMsg))
                return true;
            return false;
        }

        /// <summary>
        /// 保存日志
        /// </summary>
        /// <param name="module">模块名称</param>
        /// <param name="operate">操作名称</param>
        /// <param name="logtype">日志类型（系统管理、基础资料等）</param>
        /// <param name="detail">详细日志</param>
        protected void SaveLog(string module, string operate, string logtype, string detail)
        {
            try
            {
                S_LogDao.Save(new S_Log
                {
                    ID = Rnd.Guid.ToString(),
                    Module = module,
                    Operate = operate,
                    Detail = detail,
                    LogType = logtype,
                    OpDate = DateTime.Now,
                    CreateUser = _userInfo.CLoginInfo.UserName,
                    CreateTime = DateTime.Now,
                    ModifyUser = _userInfo.CLoginInfo.UserName,
                    ModifyTime = DateTime.Now,
                    Ip = Tools.Common.Tools.GetIP()
                });
            }
            catch
            {
            }
        }
    }
}
